package com.csit.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.csit.bean.Pager;
import com.csit.dao.CardChargeDao;
import com.csit.dao.CustomerCardDao;
import com.csit.entity.CardCharge;
import com.csit.entity.Customer;
import com.csit.entity.CustomerCard;
import com.csit.service.CardChargeService;
import com.csit.util.ListAndJsonUtil;
import com.csit.util.ResponseMsgUtil;

@Service
public class CardChargeServiceImpl extends BaseServiceImpl<CardCharge, Integer> implements CardChargeService {

	@Resource
	private CardChargeDao cardChargeDao;
	@Resource
	private CustomerCardDao customerCardDao;
	
	@Resource
	public void setBaseDao(CardChargeDao cardChargeDao) {
		super.setBaseDao(cardChargeDao);
	}
	
	public ResponseMsgUtil addCardCharge(CardCharge cardCharge,Integer customerID) {
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(customerID==null){
			msgUtil.setMsg("客户不能为空");
			return msgUtil;
		}
		if(cardCharge.getChargeDate()==null){
			msgUtil.setMsg("充值日期不能为空");
			return msgUtil;
		}
		if(cardCharge.getActualAmount()==null){
			msgUtil.setMsg("充值金额不能为空");
			return msgUtil;
		}
		Customer customer = new Customer();
		customer.setCustomerID(customerID);
		CustomerCard card = customerCardDao.getCustomerCardByCustomer(customer);
		cardCharge.setCustomerCard(card);
		Double freeAmount = cardCharge.getFreeAmount()==null?0d:cardCharge.getFreeAmount();
		card.setAmount(card.getAmount()+cardCharge.getActualAmount()+freeAmount);
		card.setScore(cardCharge.getActualAmount().intValue()+card.getScore());
		saveEntity(cardCharge);
		msgUtil.setSuccess(true);
		return msgUtil;
	}

	public ResponseMsgUtil deleteCardCharge(Integer chargeID) {
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		CardCharge cardCharge = getEntity(chargeID);
		CustomerCard card = customerCardDao.get(cardCharge.getCustomerCard().getCardID());
		Double freeAmount = cardCharge.getFreeAmount()==null?0d:cardCharge.getFreeAmount();
		card.setAmount(card.getAmount()-cardCharge.getActualAmount()-freeAmount);
		card.setScore(card.getScore()-cardCharge.getActualAmount().intValue());
		deleteEntity(cardCharge);
		msgUtil.setSuccess(true);
		return msgUtil;
	}

	public String queryCardCharge(Integer pageNumber, Integer pageSize,Integer customerID, Date beginDate, Date endDate) {
		Pager pager = new Pager(pageNumber, pageSize);
		CardCharge cardCharge = new CardCharge();
		
		if(customerID!=null){
			Customer customer = new Customer();
			customer.setCustomerID(customerID);
			CustomerCard card = customerCardDao.get("customer", customer);
			cardCharge.setCustomerCard(card);
		}
		pager = cardChargeDao.query(pager, cardCharge, beginDate, endDate);
		String[] propertiesName = {"chargeID","chargeDate","actualAmount","freeAmount","operate.userName","customerCard.customerID",
								   "customerCard.customerCode","customerCard.customerName","customerCard.cardCode","customerCard.cardID"};
		String jsonArray = ListAndJsonUtil.getJSONString(pager.getList(), propertiesName, pager.getTotalCount());
		return jsonArray;
	}

	public ResponseMsgUtil updateCardCharge(CardCharge cardCharge) {
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);

		if(cardCharge.getChargeDate()==null){
			msgUtil.setMsg("充值日期不能为空");
			return msgUtil;
		}
		if(cardCharge.getActualAmount()==null){
			msgUtil.setMsg("充值金额不能为空");
			return msgUtil;
		}
		
		CardCharge oldcardCharge = getEntity(cardCharge.getChargeID());
		
		CustomerCard card = oldcardCharge.getCustomerCard();
		
		Double freeAmount = cardCharge.getFreeAmount()==null?0d:cardCharge.getFreeAmount();
		
		Double oldFreeAmount = oldcardCharge.getFreeAmount()==null?0d:oldcardCharge.getFreeAmount();
		
		card.setAmount(card.getAmount()+cardCharge.getActualAmount()+freeAmount-oldcardCharge.getActualAmount()-oldFreeAmount);
		
		card.setScore(cardCharge.getActualAmount().intValue()+card.getScore()-oldcardCharge.getActualAmount().intValue());
		
		oldcardCharge.setChargeDate(cardCharge.getChargeDate());
		oldcardCharge.setActualAmount(cardCharge.getActualAmount());
		oldcardCharge.setFreeAmount(cardCharge.getFreeAmount());
		
		msgUtil.setSuccess(true);
	
		return msgUtil;
	}

}
